import { Navigate, Outlet, useLocation } from 'react-router-dom';
import { routers } from '@/router';
import { flatRoutes } from '@/utils/routeUtils';
import { NOTFOUND } from '@/config/url';
/**
 * 路由守卫
 * 1. 菜单是后端返回的，所以不会存在403页面的问题
 * 2. 是否登录这个判断逻辑是否需要配置在路由上？TODO - 目前是所有页面没登录都重定向到登录页
*/
// const NEED_LOGIN_URL = [ACCOUNT, ASK, SEARCH, MY_QA_DETAIL, ANSWER_GOLD, PICK_UP, SETTING, FAVORITES, BROWSING, MESSAGE];

const PrivateRoute = ({ element: Component, ...rest }) => {
    const baseRouters = routers();
    let baseRoutes = flatRoutes(baseRouters),
        appRoutes = window.SITE_CONFIG?.appRouteArray || [],
        location = useLocation(),
        NotFound = baseRoutes.find(i => i.url === NOTFOUND)?.component || null,
        existPage = appRoutes.find(i => i.url === location.pathname);

    return existPage ? <Component {...rest} /> : <NotFound />;
};

export default PrivateRoute;
